// Copyright (C) 2016 the V8 project authors. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
esid: sec-promise.any
description: Rejecting through immediate invocation of the provided resolving function
info: |
    ...
    Let promiseCapability be NewPromiseCapability(C).
    ...
    Let result be PerformPromiseAny(iteratorRecord, promiseCapability, C).
    ...

    Runtime Semantics: PerformPromiseAny
    ...
    8. Repeat
       ...
       r. Perform ? Invoke(nextPromise, "then",
          « resultCapability.[[Resolve]], rejectElement »).


    Promise.any Reject Element Functions
    ...
    6. Return RejectPromise(promise, reason).
flags: [async]
features: [Promise.any, arrow-function]
---*/

function Test() {
    let callCount = 0;
    let thenable = {
      then(_, reject) {
        callCount++;
        reject('reason');
      }
    };

    Promise.any([thenable])
      .then(() => {
        $DONE('The promise should not be fulfilled.');
      }, (error) => {
        assert.sameValue(callCount, 1, "callCount === 1");
        assert(error instanceof AggregateError, "error instanceof AggregateError");
        assert.sameValue(error.errors[0], "reason", "error.errors[0] === 'reason'");
        $DONE();
      });
}

// preheat
const NUMBER_OF_PREHEAT = 1;
for (let i = 0; i < NUMBER_OF_PREHEAT; i++) {
    Test();
}

ArkTools.jitCompileAsync(Test);
assert.sameValue(ArkTools.waitJitCompileFinish(Test), true, '[JIT] JIT disabled.');

// test
const NUMBER_OF_TEST = 1; // or 2
for (let i = 0; i < NUMBER_OF_PREHEAT; i++) {
    Test();
}
